<template>
  <div class="content-item">
    <TableModel :table-data="workAreaList" :loading="loading" :is-show-select=false>
      <el-table-column prop="name" label="工区名称"></el-table-column>
      <el-table-column prop="bidSectionName" label="所属标段"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editWorkAreaInfo(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="delWorkAreaInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </TableModel>
    <Pagination :pageData="pageData" @pageNum="changPageNum" @pageSize="onChangePageSize" />

    <el-dialog :title="dialogTitle" width="500px" :visible.sync="dialogFormVisible">
      <el-form>
        <div class="form-item">
          <span>工区名称：</span>
          <el-input class="form-width" size="medium" v-model.trim="name" placeholder="请输入" />
        </div>
        <div class="form-item">
          <span>标段：</span>
          <el-select v-model="bidSection" size="medium" class="form-width" clearable placeholder="请选择">
            <el-option v-for="item in biaoduanList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitFormInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TableModel from "../../models/TableModel";
import Pagination from "../Pagination";
import { messageBox } from "../../../util/utils";
import httpInterface from "../../../util/httpInterface";
import { mapState } from "vuex";

export default {
  name: "WorkAreaList",
  components: { TableModel, Pagination },
  data() {
    return {
      workAreaList: [],
      loading: false,
      pageData: {
        total: 0,
        pageNum: 1,
        pageSize: 20
      },
      dialogTitle: "添加工区信息",
      dialogFormVisible: false,
      name: "",
      bidSection: "",
      id: ""
    }
  },

  computed: {
    ...mapState(["biaoduanList"])
  },

  methods: {
    showDailog() {
      this.dialogTitle = "添加工区信息";
      this.dialogFormVisible = true;
      this.name = "";
      this.bidSection = "";
      this.id = "";
    },

    changPageNum (page) {
      this.pageData.pageNum = Number(page);
      this.getWorkAreaList()
    },

    onChangePageSize (size) {
      this.pageData.pageSize = Number(size);
      this.getWorkAreaList();
    },

    getWorkAreaList() {
      let { pageData, biaoduanList } = this;
      let params = {
        page: pageData.pageNum,
        size: pageData.pageSize
      };
      httpInterface.requestGet(this, httpInterface.URL_WORKAREA_LIST, params, res => {
        let { other, success } = res.data;
        let { rows, total } = other;
        rows.forEach(item => {
          biaoduanList.forEach(key => {
            if (item.bidSection == key.id) {
              item.bidSectionName = key.name;
            }
          })
        })
        this.workAreaList = rows.reverse();
        this.pageData.total = total;
      }, null)
    },

    editWorkAreaInfo(id) {
      this.dialogTitle = "编辑工区信息";
      this.dialogFormVisible = true;
      this.id = id;
      let params = { id };
      httpInterface.requestGet(this, httpInterface.URL_WORKAREA_INFO, params, res => {
        let { other } = res.data;
        this.bidSection = other.bidSection;
        this.name = other.name;
      }, null)
    },

    delWorkAreaInfo(id) {
      messageBox(() => {
        let params = { id };
        httpInterface.requestGet(this, httpInterface.URL_WORKAREA_DELETE, params, res => {
          let { success } = res.data;
          if ( success ) {
            this.getWorkAreaList();
            this.successMessage("删除成功！");
          } else {
            this.errorMessage("删除失败！");
          }
        }, null)
      })
    },

    verifyFormInfo() {
      let { bidSection, name } = this;
      if (!name) {
        this.warningMessage("请输入工区名称！");
        return false;
      }
      if (!bidSection) {
        this.warningMessage("请选择标段！");
        return false;
      }
      return true;
    },

    submitFormInfo() {
      if (!this.verifyFormInfo()) {
        return true;
      }
      let { bidSection, name, id } = this;
      let params = { name, bidSection };
      if (id) {
        params.id = id
      }
      let url = id ? httpInterface.URL_WORKAREA_UPDATE : httpInterface.URL_WORKAREA_ADD;
      httpInterface.requestPost(this, url, params, res => {
        let { success } = res.data;
        if (success) {
          this.dialogFormVisible = false;
          if (id) {
            this.successMessage("编辑成功");
          } else {
            this.successMessage("添加成功");
          }
          this.getWorkAreaList();
        } else {
          if (id) {
            this.errorMessage("编辑失败");
          } else {
            this.errorMessage("添加失败");
          }
        }
      }, null, false)
    }
  }
}
</script>

<style scoped lang="scss">
.form-item {
  display: flex;
  margin: 16px 0px;
  justify-content: center;
  align-items: center;

  span {
    font-size: 14px;
    margin-right: 10px;
    display: inline-block;
    width: 70px;
    text-align: right;
  }

  .form-width{
    width: 230px;
  }
}
</style>
